<?php


class ConnectionOption
{
    const Host          = 'host';
    const Port          = 'port';
    const DatabaseName  = 'database';
    const Login         = 'login';
    const Password      = 'password';
}

interface IConnection
{
    /**
     * @abstract
     * @return void
     */
    function Open();

    /**
     * @abstract
     * @return void
     */
    function Close();

    /**
     * @abstract
     * @return boolean
     */
    function Opened();

    /**
     * @abstract
     * @param string $sql
     * @return IDataReader
     */
    function ExecuteReader($sql);

    /**
     * @abstract
     * @param string $sql
     * @return void
     */
    function ExecuteNoResult($sql);

    /**
     * @abstract
     * @param string $sql
     * @return mixed
     */
    function ExecuteScalar($sql);
}

interface IDataRecord
{
    /**
     * @abstract
     * @param string $fieldName
     * @return mixed
     */
    function GetFieldValueByName($fieldName);

    /**
     * @abstract
     * @param string $fieldName
     * @return boolean
     */
    function IsPrimaryKeyField($fieldName);

    /**
     * @abstract
     * @return string[]
     */
    function GetFieldNames();
}

interface IDataReader extends IDataRecord
{
    /**
     * @abstract
     * @return void
     */
    function Open();

    /**
     * @abstract
     * @return void
     */
    function Close();

    /**
     * @abstract
     * @return boolean
     */
    function Next();
}

interface IEditableDataRecord extends IDataRecord
{
    /**
     * @abstract
     * @param string $fieldName
     * @param mixed $value
     * @return void
     */
    function SetFieldValueByName($fieldName, $value);

    /**
     * @abstract
     * @param string $fieldName
     * @param string $fileName
     * @return void
     */
    function SetFieldValueByNameAsFileName($fieldName, $fileName);
}

interface IDataEditor extends IDataReader, IEditableDataRecord
{
    /**
     * @abstract
     * @return void
     */
    function Edit();

    /**
     * @abstract
     * @return void
     */
    function Insert();

    /**
     * @abstract
     * @return void
     */
    function Post();
}

class ConditionOperator
{
    public static $Equals;

    public static function Init()
    {
        self::$Equals = new ConditionOperator('=');
    }

    /** @var string */
    private $shortName;

    /**
     * @param string $shortName
     */
    public function __construct($shortName)
    {
        $this->shortName = $shortName;
    }

    /**
     * @return string
     */
    public function GetShortName() { return $this->shortName; }
}
ConditionOperator::Init();

interface ICriteria
{
    function AddSimpleCondition($fieldName, ConditionOperator $operator, $value);
}

interface IDataset extends IDataEditor, ICriteria
{ }

class ConnectionFailedException extends Exception
{
    public function __construct($message)
    {
        parent::__construct($message);
    }
}

class SqlException extends Exception
{
    public function __construct($message)
    {
        parent::__construct($message);
    }
}

/**
 * @param IDataRecord $record
 * @return string[]
 */
function GetPrimaryKeyFieldNames(IDataRecord $record)
{
    $result = array();
    foreach($record->GetFieldNames() as $fieldName)
    {
        if ($record->IsPrimaryKeyField($fieldName))
            $result[] = $fieldName;
    }
    return $result;
}

/**
 * @param IDataRecord $record
 * @return string[]
 */
function GetPrimaryKeyValues(IDataRecord $record)
{
    $result = array();

    foreach($record->GetFieldNames() as $fieldName)
    {
        if ($record->IsPrimaryKeyField($fieldName))
            $result[] = $record->GetFieldValueByName($fieldName);
    }

    return $result;
}


?>